package com.zhou.controller;

import com.alibaba.fastjson.JSON;
import com.zhou.bean.Express;
import com.zhou.bean.Message;
import com.zhou.bean.ResultData;
import com.zhou.bean.nExpress;
import com.zhou.mvc.ResponseBody;
import com.zhou.service.ExpressService;
import com.zhou.utils.DateFormatUtil;
import com.zhou.utils.UserUtil;

import javax.json.Json;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = ExpressService.console();
        Message msg = new Message();
        if (data.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSON.toJSONString(msg);
        return json;
    }
    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        String pageIndex = req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        List<Express> list = ExpressService.findAll(true, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        List<nExpress> list1 = new ArrayList<>();
        for (Express express : list) {
            String inTime = DateFormatUtil.format(express.getInTime());
            String outTime = express.getOutTime()==null?"未出库":DateFormatUtil.format(express.getOutTime());
            String status = express.getStatus()==0?"未取件":"已取件";
            String code = express.getCode()==null?"已取件":express.getCode();
            nExpress nExpress = new nExpress(express.getId(), express.getNumber(), express.getUsername(), express.getUserPhone(), express.getCompany(), code,inTime,outTime,status,express.getSysPhone());
            list1.add(nExpress);
        }
        List<Map<String, Integer>> console = ExpressService.console();
        int total = console.get(0).get("data1_size");
        //集合封装为bootstrap识别的数据格式
        ResultData<nExpress> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSON.toJSONString(data);
        return json;

    }
    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        Express e = new Express(number,username,userPhone,company,UserUtil.getUserPhone(req.getSession()));
        System.out.println(e);
        boolean insert = ExpressService.insert(e);
        Message message = new Message();
        if (insert){
            message.setStatus(0);
            message.setResult("快递录入成功");
        }else {
            message.setStatus(-1);
            message.setResult("快递录入失败");
        }
        return JSON.toJSONString(message);
    }
    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Express express = ExpressService.findByNumber(number);
        //System.out.println(express);
        Message msg = new Message();
        if (express.getNumber()==null){
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(express);
        }
       return JSON.toJSONString(msg);
    }
    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        boolean delete = ExpressService.delete(Integer.parseInt(id));
        Message message = new Message();
        if (delete){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JSON.toJSONString(message);
    }
    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String status = req.getParameter("status");
        String userPhone = req.getParameter("userPhone");
        Express express = new Express(number,username,userPhone,company,Integer.parseInt(status));
        boolean update = ExpressService.update(Integer.parseInt(id), express);
        Message msg = new Message();
        if (update){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        return JSON.toJSONString(msg);
    }
}
